import { Component } from 'labrador-immutable';
import { bindActionCreators } from 'redux';
import { connect } from 'labrador-redux';
import { createAction } from 'redux-actions';

class Index extends Component {

    state = {
        activeId: 0,
    }

    onLoad(options) {
        this.props.fetchClassify()
        this.fetchProducts(0);
    }

    searchProducts(event) {
        const cateId = event.currentTarget.dataset.cateId;
        this.setState({ activeId: cateId }, () => {
            this.fetchProducts(0);
        })
    }

    async fetchProducts(pageNum) {
        this.props.fetchProducts({ cateId: this.state.activeId, pageNum: pageNum })
    }

    handleReachBottom() {
        let { pageNum, end } = this.props;
        if (end) {
            return;
        }
        pageNum++;
        this.fetchProducts(pageNum);
    }

    productItemClick(event) {
        const id = event.currentTarget.dataset.id
        wx.navigateTo({ url: '/pages/resourcelist/index?id=' + id });
    }

    // 设置分享
    onShareAppMessage() {
        return {
            title: this.props.info.title,
            desc: '萌酱酱选辅食一个致力于帮助新手爸妈为宝宝轻松制作辅食的APP。',
            path: '/pages/resource/index',
        }
    }
}

export default connect(
    ({ resource }) => ({ cates: resource.cates, list: resource.list, end: resource.end, pageNum: resource.pageNum }),
    (dispatch) => bindActionCreators({
        fetchClassify: createAction('resource/fetchClassify', (params) => (params)),
        fetchProducts: createAction('resource/fetchProducts', (params) => (params)),
    }, dispatch)
)(Index);